<?php
/**
 * user.inc.php
 */

if(!defined('IN_SITE') || !defined('IN_ADMIN') || $administrator['groupid'] != 1) {
	exit('Access Denied');
}

$op = in_array($_GET['op'], array('add', 'list', 'edit', 'editsave', 'del', 'dboptimize', 'admingroup')) ? $_GET['op'] : 'list';

switch ($op)
{
	case 'add':

		if($adminadd) {
			$name = $pass = '';
			if (isset($_POST['name']) && !empty($_POST['name']) && isset($_POST['pass']) && !empty($_POST['pass'])) {
				$name = $_POST['name'];
				$editor = $_POST['editor'];
				$pass = md5($_POST['pass']);
				$groups = explode('|', $_POST['groups']);

				$sql = "INSERT INTO `{$tablepre}admin` (name , editor, pass , groupid , grouptype, grouptitle , pubtime) VALUES ('$name' , '$editor', '$pass' , '$groups[0]' , '$groups[1]' , '$groups[2]' , '$timestamp')";
				$db->query($sql) ? showmessage('添加管理员成功。', '?action=user&op=list') : showmessage('添加管理员失败。');
			} else {
				showmessage('请将信息填写完整。');
			}
		}

		break;

	case 'list':
		$adminRes = $db->query("SELECT id , name , editor, pubtime , groupid , grouptitle FROM `{$tablepre}admin` ORDER BY id");
		while ($row = $db->fetch_array($adminRes)) {
			$adminArr[] = $row;
		}

		break;

	case 'edit':
		$id = '';
		if (isset($_GET['id']) && !empty($_GET['id']) && $id = $_GET['id']) {
			$adminRes = $db->query("SELECT id , name , editor, groupid , grouptype, grouptitle FROM `{$tablepre}admin` WHERE id = '$id'");
			$adminArr = $db->fetch_array($adminRes);
		}
		break;

	case 'editsave':
		$id = $name = $pass = $groups = $grouptype = '';
		if (isset($_POST['name']) && trim($_POST['name']) != '' && isset($_POST['id']) && $_POST['id'] != '') {
			$id = $_POST['id'];
			$name = $_POST['name'];
			$editor = $_POST['editor'];
			$pass = $_POST['pass'] != '' ? md5($_POST['pass']) : '';
			$groups = explode('|' , $_POST['groups']);
			$spass = $pass != '' ? " , pass = '$pass' " : '';
			if ($db->query("UPDATE `{$tablepre}admin` SET name = '$name' , editor = '$editor', groupid = '$groups[0]' , grouptype='$groups[1]', grouptitle = '$groups[2]' $spass WHERE id = '$id'")){
				showmessage('修改管理员成功。' , '?action=user&op=list');
			} else {
				showmessage('操作失败。');
			}
		}
		break;

	case 'del':
		if (!empty($_POST['delsubmit']) && !empty($_POST['id'])){
			foreach ($id as $val) {
				$db->query("DELETE FROM {$tablepre}admin WHERE id='$val'");
			}
			showmessage("删除成功", "?action=user&op=list");
		}
		break;
		
	case 'dboptimize':
		if($optimizesubmit)
		{
			$query = $db->query("SHOW TABLE STATUS", 'SILENT');
			while($table = $db->fetch_array($query)) {
				if(is_array($optimizetables) && in_array($table['Name'], $optimizetables)) {
					$q = $db->query("OPTIMIZE TABLE $table[Name]");
				}
			}
			if($q) showmessage("优化完成", "?action=user&op=dboptimize");
		}
		else
		{
			$query = $db->query("SHOW TABLE STATUS", 'SILENT');
			while($table = $db->fetch_array($query)) {
				if($table['Data_free']) {
					$table['checked'] = $table['Engine'] == 'MyISAM' ? 'checked' : 'disabled';
					$dbtable[] = $table;
					$totalsize += $table['Data_length'] + $table['Index_length'];
				}
			}
		}
		break;
}

include admin_tpl("user");
?>